-- Calculate the number of sections
module Hexena.Cmds.CalcSessions (run) where

import Data.Array.Unboxed
import qualified Data.ByteString.Lazy as L
import Data.IFF
import Data.Word
import System.Environment (getArgs)

import Hexena.Chunks
import Hexena.Math.Statistics
import Hexena.PEHeader

run' :: Either L.ByteString T -> Word16
run' (Left d) = peNumberOfSections $ readPEFrom d
run' (Right d) = peNumberOfSections $ loadPEHD $ mapIFF d

runner :: String -> IO Word16
runner x = do d <- loadMalwareFile x
           let s = run' d
           putStrLn $ x ++ "\t" ++ show s
           return s

accumulate :: [Word16] -> UArray Word16 Int
accumulate = accumArray (+) 0 (0, 65535) . map (\x -> (x,1)) 

printFun :: (Word16, Int) -> String
printFun (x, y) = show x ++ "\t" ++ show y

{--
main :: IO ()
main = do xs <- mapM run =<< getArgs
          let arr = accumulate xs
              list0 = filter ((/= 0) . snd) $ assocs arr
          print list0
          mapM_ (putStrLn . printFun) list0
          print $ meanStdev $ map (fromIntegral . fst) list0
          print $ sum $ map snd list0
--}

run :: FilePath -> IO ()
run fn = runner fn >>= \xs -> accumulate xs >>=
         \arr -> filter ((/= 0) . snd) $ assocs arr >>=
         \list0 -> print list0 >>
         mapM_ (putStrLn . printFun) list0 >>
         print $ meanStdev $ map (fromIntegral . fst) list0 >>
         print . sum $ map snd list0
